// personal_center/pages/add_address/index.js
import AddressParse from '/address-parse';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text_value: '',
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    checked: true,
    recipient_name:'',
    recipient_phone:'',
    recipient_address:'',
    recipient_district:'',
    recipient_city:'',
    recipient_state:'',
    type:1,
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
      recipient_state:e.detail.value[0],
      recipient_city:e.detail.value[1],
      recipient_district:e.detail.value[2]
    })
  },
  
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  paste(){
    const that=this;
    wx.getClipboardData({
      success (res){
        that.setData({
          text_value:res.data
        })
        that.analysis()
      }
    })
  },
  analysis(){
    const [result] = AddressParse.parse(this.data.text_value);
    this.setData({
      recipient_state:result.province,
      recipient_city:result.city,
      recipient_district:result.area,
      region:[result.province,result.city,result.area],
      recipient_name:result.name,
      recipient_phone:result.mobile,
      recipient_address:result.details
    })
  },
  get_name(e){
    this.setData({
      recipient_name:e.detail.value
    })
  },
  get_phone(e){
    this.setData({
      recipient_phone:e.detail.value
    })
  },
  get_address(e){
    this.setData({
      recipient_address:e.detail.value
    })
  },
  confirm(){
    const that=this;
    if(!that.data.recipient_name){
      app.toast('请输入姓名')
    }else if(!that.data.recipient_phone){
      app.toast('请输入电话')
    }else if(!that.data.recipient_state){
      app.toast('请选择省市区')
    }else if(!that.data.recipient_address){
      app.toast('请输入详细地址')
    }else{
      app.loading()
      let obj={
        recipient_country:that.data.recipient_country,
        recipient_zip:that.data.recipient_zip,
        recipient_name:that.data.recipient_name,
        recipient_phone:that.data.recipient_phone,
        recipient_district:that.data.recipient_district,
        recipient_state:that.data.recipient_state,
        recipient_city:that.data.recipient_city,
        recipient_address:that.data.recipient_address,
        type:1
      }
      if(that.data.id){
        obj.id=that.data.id
      }
      app.wxRequest('/api/Address/add',obj,function(res){
        if(res.code==200){
          app.toast('添加成功')
          wx.navigateBack({
            delta:1
          })
        }else{
          app.toast(res.message)
        }
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})